﻿using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions;
using Microsoft.Data.Sqlite;

namespace Solution.Infrastructure.Repository.Impl
{
    public abstract class AbstractSqlLiteRepository
    {
        protected virtual string ConnectionString { get; set; } = "Data Source=./Resource/solution.db;Mode=ReadWriteCreate";

        protected async Task<int> InsertAsync<T>(T entity, IDbTransaction transaction = null, int commandTimeout = 300, string connString = "") where T : class
        {
            using (var conn = GetSqliteConnection(connString))
            {
                return await conn.InsertAsync(entity, transaction, commandTimeout);
            }
        }

        protected async Task<T> GetAsync<T, TKey>(TKey id, IDbTransaction transaction = null, int commandTimeout = 300, string connString = "") where T : class
        {
            using (var conn = GetSqliteConnection(connString))
            {
                return await conn.GetAsync<T>(id, transaction, commandTimeout);
            }
        }

        public async Task<int> ExecuteAsync(string sql, object param, IDbTransaction transaction = null, int commandTimeout = 300, string connString = "")
        {
            using (var conn = GetSqliteConnection(connString))
            {
                return await conn.ExecuteAsync(sql, param, transaction, commandTimeout);
            }
        }

        public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param, IDbTransaction transaction = null, int commandTimeout = 300, string connString = "")
        {
            using (var conn = GetSqliteConnection(connString))
            {
                return await conn.QueryAsync<T>(sql, param, transaction, commandTimeout);
            }
        }

        public async Task<T> ExecuteScalarAsync<T>(string sql, object param, IDbTransaction transaction = null, int commandTimeout = 300, string connString = "")
        {
            using (var conn = GetSqliteConnection(connString))
            {
                return await conn.ExecuteScalarAsync<T>(sql, param, transaction, commandTimeout);
            }
        }

        public async Task<T> QueryFirstOrDefault<T>(string sql, object param, IDbTransaction transaction = null, int commandTimeout = 300, string connString = "")
        {
            using (var conn = GetSqliteConnection(connString))
            {
                return await conn.QueryFirstOrDefaultAsync(sql, param, transaction, commandTimeout);
            }
        }

        public async Task<T> QuerySingleOrDefault<T>(string sql, object param, IDbTransaction transaction = null, int commandTimeout = 300, string connString = "")
        {
            using (var conn = GetSqliteConnection(connString))
            {
                return await conn.QuerySingleOrDefault(sql, param, transaction, commandTimeout);
            }
        }

        private SqliteConnection GetSqliteConnection(string conn)
        {
            if (string.IsNullOrEmpty(conn))
            {
                conn = ConnectionString;
            }

            return new SqliteConnection(conn);
        }
    }
}